# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import requests
from qunar import settings
import os
import MySQLdb  

class ImageDownloadPipeline(object):
    def process_item(self, item, spider):
           db = MySQLdb.connect("localhost","root","mypassword","test_db" )  
           cursor = db.cursor()  
           if 'image_urls' in item:
                images = []
                dir_path = '%s/%s/%s' % (settings.IMAGES_STORE,item['place'],item['name'])
                ##item['url'] = <200 www.explame.com>
                urls = str(item['url'])[5:-1]

                if not os.path.exists(dir_path):
                    os.makedirs(dir_path)
                for image_url in item['image_urls']:
                    image_file_name = image_url[-15:]
                    file_path = '%s/%s' % (dir_path, image_file_name)
                    images.append(file_path)
                    if os.path.exists(file_path):
                         continue
                    with open(file_path, 'wb') as handle:
                         response = requests.get(image_url, stream=True)
                         for block in response.iter_content(1024):
                             if not block:
                                 break
                             handle.write(block)

                    sql = "INSERT INTO tour_tbl (tour_title,tour_icourl,local_url) VALUES ( '%s','%s','%s' )" % ( image_file_name,image_url,urls)  
                    try:  
                       # 执行sql语句  
                       cursor.execute(sql)  
                       # 提交到数据库执行  
                       db.commit()  
                    except:  
                       # 发生错误时回滚  
                       db.rollback()            

                item['images'] = images

            # 关闭数据库连接  
           db.close()  
           return item


  